﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using SingleUserMall.Common;
using SingleUserMall.Workers.VO;
using System.Linq;
using System.Threading.Tasks;

namespace SingleUserMall.Workers
{
    [AbpAuthorize]
    public class WorkerAppService : SingleUserMallAppServiceBase, IWorkerAppService
    {
        private readonly IRepository<Worker,long> _workerRepository;
        public WorkerAppService(
            IRepository<Worker,long> workerRepository)
        {
            _workerRepository = workerRepository;
        }
        public async Task<PagedResultDto<WorkerViewVO>> GetWorkersAsync(PagedForm form)
        {
            var workers = _workerRepository.GetAll().OrderByDescending(x=> x.Installation).ThenByDescending(x=> x.WorkingYear).ThenByDescending(x => x.WorkerStar);
            return await workers.ToPagedResultDtoAsync<Worker, WorkerViewVO>(form,(f, vo) =>
            {
                vo.FeedbackRate = f.WorkerStar * 20 + "%";
            });
        }

        public async Task<WorkerViewVO> GetWorkerAsync(SingleId<long> form)
        {
            var worker = await  _workerRepository.FirstOrDefaultAsync(form.Id);
            return  worker.ToVO<Worker, WorkerViewVO>((w, vo) =>
            {
                vo.FeedbackRate = w.WorkerStar * 20 + "%";
            });
        }
    }
}
